# coding=gbk

from datetime import date, timedelta
from DataAccess.DBConnFactory import DBConnFactory
from Import.TradeImport import *
from Import.PositionImport import *
from Position.CalcDailyPosition import *
from Recover.PositionRecover import *
from Config.ImportConfig import *

def test_case2():
	conn = DBConnFactory().get_db_connection('PKEDB')
	cursor = conn.cursor()
	
	#clear db data
	clear_all()
	print 'info, clear all data complete.'

	#add portfolio info entry
	sql_text = '''delete from portfolio_info where portfolio_id='SCTestD' '''
	cursor.execute(sql_text)
	conn.commit()
	sql_text = '''insert into portfolio_info(portfolio_id,"name",inception_date,inception_val,currency,manager,"type",cash_account) 
				values('SCTestD','SCTestD',TO_DATE('2012-05-29','yyyy-mm-dd'),0,'USD','XieBing','PUBLIC',0) '''
	cursor.execute(sql_text)
	conn.commit()
	print 'info, add test portfolio info complete.'
	

	sql_text = '''insert into position 
				(ref_date,portfolio_id,ticker,security_type,amount,avg_cost_price,price_currency)
				values(TO_DATE(:1,'yyyy-mm-dd'),:2,:3,:4,:5,:6,:7) '''
	v =			[('2012-05-29','SCTestD','9999999','CASH',0,1.0,'USD'),
				('2012-05-29','SCTestD','9999999','CASH',0,1.0,'HKD'),
				('2012-05-29','SCTestD','9999999','CASH',0,1.0,'CNY'),
				('2012-05-29','SCTestD','08102','STOCK',-1111111,1.0,'HKD'),
				('2012-05-29','SCTestD','900949','STOCK',2222222,2.0,'USD'),
				('2012-05-29','SCTestD','002382','STOCK',-3333333,3.0,'CNY'),
				('2012-05-29','SCTestD','XS0464501329','PNOTE',4444444,4.0,'USD')]				
	cursor.executemany(sql_text, v)
	
	sql_text = '''insert into pnote_info values ('XS0464501329','300217','USD','CNY') '''
	cursor.execute(sql_text)		
	conn.commit()
	print 'info, add initial position complete.'
	
	trade_date = date(2012, 5, 30)	
	sql_text = '''insert into transaction(trade_date,ticker,trade_type,portfolio_id,security_type,amount,cost_price,currency) 
				values(TO_DATE('2012-05-30','yyyy-mm-dd'),'002382','SHORT SELLING','SCTestD','STOCK',1111111,2.00,'CNY')'''
	cursor.execute(sql_text)
	conn.commit()
	
	update_position_driven_by_dividend(trade_date)
	print 'info, 2012-05-30 dividend settlement complete.'
	update_position_driven_by_transaction(trade_date)
	update_position_driven_by_repo_expire(trade_date)
	print 'info, calculate 2012-05-30 position complete.'
	
	sql_text = '''delete from portfolio_info where portfolio_id='SCTestD' '''
	cursor.execute(sql_text)
	conn.commit()

	
	